/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package lyra;

import javax.persistence.EntityTransaction;
import javax.persistence.Query;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import lyra.pojos.*;

/**
 *
 * @author Koustas, Marmaridis, Mariolou
 */
public class FListMusicGroup extends javax.swing.JInternalFrame {

    /**
     * Creates new form frmListArtist
     */
    
    class SelectionListener implements ListSelectionListener {
    JTable table;
        
    SelectionListener(JTable table) {
        this.table = table;
    }
    
    @Override
       public void valueChanged(ListSelectionEvent event) {
        if (table.getSelectedRowCount() == 1) {
            table.getValueAt(table.getSelectedRow(), 0);
        
            try {
//                RefreshArtistDetails( (Long) ArtistTable.getValueAt(ArtistTable.getSelectedRow(),0));
            } catch (Exception e) {

            } finally {

            }
        }
    }
  }

    javax.swing.JDesktopPane desktop;
    private FMusicGroup  fmusicgroup;
    mainForm _parentFrame;

     public void EnableDisableForm(boolean enable) {
        btnDelete.setEnabled(enable);
        btnExit.setEnabled(enable);
        btnInsert.setEnabled(enable);
        btnUpdate.setEnabled(enable);
        MusicGroupTable.setEnabled(enable);
    }
     
    public FListMusicGroup(javax.swing.JDesktopPane fdesktop, mainForm parentFrame) {
        initComponents();
        desktop = fdesktop;
        SelectionListener listener = new SelectionListener(this.MusicGroupTable);
        this.MusicGroupTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        this.MusicGroupTable.getSelectionModel().addListSelectionListener(listener);
        this.MusicGroupTable.setCellSelectionEnabled(false); 
        this.MusicGroupTable.setRowSelectionAllowed(true);     
        LYRA.setFrameIcon(this,"WindowsGroups_7309.png");
        _parentFrame = parentFrame;
        _parentFrame.EnableDisableMenu(false);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        bindingGroup = new org.jdesktop.beansbinding.BindingGroup();

        LYRAPUEntityManager0 = java.beans.Beans.isDesignTime() ? null : javax.persistence.Persistence.createEntityManagerFactory("LYRAPU").createEntityManager();
        musicGroupQuery = java.beans.Beans.isDesignTime() ? null : LYRAPUEntityManager0.createQuery("SELECT a FROM Musicgroup a");
        musicGroupList = java.beans.Beans.isDesignTime() ? java.util.Collections.emptyList() : musicGroupQuery.getResultList();
        jScrollPane1 = new javax.swing.JScrollPane();
        MusicGroupTable = new javax.swing.JTable();
        btnInsert = new javax.swing.JButton();
        btnDelete = new javax.swing.JButton();
        btnUpdate = new javax.swing.JButton();
        btnExit = new javax.swing.JButton();

        setClosable(true);
        setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
        setTitle("Πίνακας συγκροτημάτων");
        addInternalFrameListener(new javax.swing.event.InternalFrameListener() {
            public void internalFrameActivated(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameClosed(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameClosing(javax.swing.event.InternalFrameEvent evt) {
                formInternalFrameClosing(evt);
            }
            public void internalFrameDeactivated(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameDeiconified(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameIconified(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameOpened(javax.swing.event.InternalFrameEvent evt) {
            }
        });

        MusicGroupTable.setAutoCreateRowSorter(true);
        MusicGroupTable.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED, null, new java.awt.Color(102, 255, 255), null, null));
        MusicGroupTable.setColumnSelectionAllowed(true);
        MusicGroupTable.setGridColor(new java.awt.Color(204, 255, 255));
        MusicGroupTable.getTableHeader().setReorderingAllowed(false);

        org.jdesktop.swingbinding.JTableBinding jTableBinding = org.jdesktop.swingbinding.SwingBindings.createJTableBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, musicGroupList, MusicGroupTable);
        org.jdesktop.swingbinding.JTableBinding.ColumnBinding columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${musicgroupid}"));
        columnBinding.setColumnName("Musicgroupid");
        columnBinding.setEditable(false);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${name}"));
        columnBinding.setColumnName("Ονομασία συγκροτήματος");
        columnBinding.setEditable(false);
        bindingGroup.addBinding(jTableBinding);
        jTableBinding.bind();
        jScrollPane1.setViewportView(MusicGroupTable);
        MusicGroupTable.getColumnModel().getSelectionModel().setSelectionMode(javax.swing.ListSelectionModel.SINGLE_INTERVAL_SELECTION);
        if (MusicGroupTable.getColumnModel().getColumnCount() > 0) {
            MusicGroupTable.getColumnModel().getColumn(0).setMinWidth(0);
            MusicGroupTable.getColumnModel().getColumn(0).setMaxWidth(0);
        }

        btnInsert.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icons/add.gif"))); // NOI18N
        btnInsert.setText("Εισαγωγή");
        btnInsert.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnInsertActionPerformed(evt);
            }
        });

        btnDelete.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icons/delete.png"))); // NOI18N
        btnDelete.setText("Διαγραφή");
        btnDelete.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnDeleteActionPerformed(evt);
            }
        });

        btnUpdate.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icons/edit.png"))); // NOI18N
        btnUpdate.setText("Μεταβολή");
        btnUpdate.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnUpdateActionPerformed(evt);
            }
        });

        btnExit.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icons/exit.png"))); // NOI18N
        btnExit.setText("Έξοδος");
        btnExit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnExitActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(btnInsert)
                .addGap(18, 18, 18)
                .addComponent(btnDelete)
                .addGap(18, 18, 18)
                .addComponent(btnUpdate)
                .addGap(18, 18, 18)
                .addComponent(btnExit)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addContainerGap()
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 474, Short.MAX_VALUE)
                    .addContainerGap()))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(401, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnInsert)
                    .addComponent(btnDelete)
                    .addComponent(btnUpdate)
                    .addComponent(btnExit))
                .addContainerGap())
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addContainerGap()
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 386, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addContainerGap(44, Short.MAX_VALUE)))
        );

        bindingGroup.bind();

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnUpdateActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnUpdateActionPerformed
        try {
            Musicgroup musicgroup = (Musicgroup) LYRA.e.createNamedQuery("Musicgroup.findByMusicgroupid").setParameter("musicgroupid", (Long) MusicGroupTable.getValueAt(MusicGroupTable.getSelectedRow(),0)).getSingleResult();
            fmusicgroup = new FMusicGroup(this, musicgroup);
            desktop.add(fmusicgroup);
            
            ///disable not occurs
            this.setEnabled(false);
            fmusicgroup.setVisible(true);
        }
        catch ( Exception ex ) {
            if (ex.getMessage()=="Invalid index")
                LYRA.MsgBox("Επιλέξτε πρώτα εγγραφή προς μεταβολή", "ΠΡΟΣΟΧΗ");
            else
                LYRA.MsgBox(ex.getMessage(), "ΠΡΟΣΟΧΗ");
        }
    }//GEN-LAST:event_btnUpdateActionPerformed

    private void btnExitActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnExitActionPerformed
        _parentFrame.EnableDisableMenu(true);
        this.dispose();
    }//GEN-LAST:event_btnExitActionPerformed

    private void btnDeleteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnDeleteActionPerformed
        try {
            DeleteMusicGroup();
        }
        catch ( Exception ex ) {
            if (ex.getMessage()=="Invalid index")
                LYRA.MsgBox("Επιλέξτε πρώτα εγγραφή προς διαγραφή", "ΠΡΟΣΟΧΗ");
            else
                LYRA.MsgBox(ex.getMessage(), "ΠΡΟΣΟΧΗ");
        }

    }//GEN-LAST:event_btnDeleteActionPerformed

    private void btnInsertActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnInsertActionPerformed
        fmusicgroup = new FMusicGroup(this);
        desktop.add(fmusicgroup);
        this.setEnabled(false);
        fmusicgroup.setVisible(true);
    }//GEN-LAST:event_btnInsertActionPerformed

    private void formInternalFrameClosing(javax.swing.event.InternalFrameEvent evt) {//GEN-FIRST:event_formInternalFrameClosing
        if (btnExit.isEnabled()) {
            _parentFrame.EnableDisableMenu(true);
            this.dispose();
        }
    }//GEN-LAST:event_formInternalFrameClosing


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.persistence.EntityManager LYRAPUEntityManager0;
    private javax.swing.JTable MusicGroupTable;
    private javax.swing.JButton btnDelete;
    private javax.swing.JButton btnExit;
    private javax.swing.JButton btnInsert;
    private javax.swing.JButton btnUpdate;
    private javax.swing.JScrollPane jScrollPane1;
    private java.util.List<lyra.pojos.Artist> musicGroupList;
    private javax.persistence.Query musicGroupQuery;
    private org.jdesktop.beansbinding.BindingGroup bindingGroup;
    // End of variables declaration//GEN-END:variables

    public void RefreshMusicGroupTable()
    {
        musicGroupQuery = LYRA.e.createQuery("SELECT a FROM Musicgroup a");
        musicGroupList = musicGroupQuery.getResultList();

        org.jdesktop.swingbinding.JTableBinding jTableBinding = org.jdesktop.swingbinding.SwingBindings.createJTableBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, musicGroupList, MusicGroupTable);
        org.jdesktop.swingbinding.JTableBinding.ColumnBinding columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${musicgroupid}"));
        columnBinding.setColumnName("MusicgroupId");
        columnBinding.setColumnClass(Long.class);
        columnBinding.setEditable(false);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${name}"));
        columnBinding.setColumnName("Ονομασία συγκροτήματος");
        columnBinding.setColumnClass(String.class);
        columnBinding.setEditable(false);
        bindingGroup.addBinding(jTableBinding);
        jTableBinding.bind();
        
        if (MusicGroupTable.getColumnModel().getColumnCount() > 0) {
            MusicGroupTable.getColumnModel().getColumn(0).setMinWidth(0);
            MusicGroupTable.getColumnModel().getColumn(0).setMaxWidth(0);
        }
        
        this.MusicGroupTable.setCellSelectionEnabled(false); 
        this.MusicGroupTable.setRowSelectionAllowed(true); 
    
        
    }
    
    private void  DeleteMusicGroup() {
        //Διαγραφή καλλιτέχνη
        if ( LYRA.yesno("Θέλετε σίγουρα να διαγραφεί το συγκρότημα " +  MusicGroupTable.getValueAt(MusicGroupTable.getSelectedRow(),1).toString().trim() ) == 0 )
        {
            try {
                EntityTransaction et = null;
                et = LYRA.e.getTransaction();
                et.begin();
                
                Query queryDelete =  LYRA.e.createNativeQuery("DELETE FROM ADMIN1.MusicGroup a WHERE a.MusicGroupID = " + (Long) MusicGroupTable.getValueAt(MusicGroupTable.getSelectedRow(),0));
                queryDelete.executeUpdate();

                et.commit();
                RefreshMusicGroupTable();
            }
            catch (Exception e) {
                LYRA.MsgBox(e.getMessage(),"ΠΡΟΣΟΧΗ");
            }
        }
    }
}
